package week11;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class CallableTest {

	public static void main(String[] args) {
		CallableTask callableTask = new CallableTask(); 
		ExecutorService executor = Executors.newCachedThreadPool();
		Future<Integer> result = executor.submit(callableTask);
		executor.shutdown();
		System.out.println("主线程正在执行");
		try {
			System.out.println("子线程执行结果:"+result.get());
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	}

}

//返回结果为Integer
class CallableTask implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		int temp = 0;
		for (int i = 0; i < 100; i++) {
			temp += i;
		}
		TimeUnit.SECONDS.sleep(2);
		return temp;
	}
	
}
